  //index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    index: 0,
    step:1,
    oldfoods: [],
    caixi1:[
      {"id": "10","name": "川菜"},
      {"id": "11","name": "粤菜"},
      {"id": "12","name": "湘菜"},
      {"id": "13","name": "鲁菜"},
      {"id": "101","name": "闽菜"},
      {"id": "102","name": "浙菜"},
      {"id": "104","name": "苏菜"},
      {"id": "105","name": "徽菜"}
    ],
    caixi2: [
      { "id": "16", "name": "西餐" },
      { "id": "17", "name": "日本料理" },
      { "id": "18", "name": "韩国料理" },
      { "id": "123", "name": "泰国菜" },
      { "id": "124", "name": "意大利菜" },
      { "id": "125", "name": "法国菜" },
      { "id": "126", "name": "东南亚菜" },
      { "id": "127", "name": "印度菜" }
    ]
  },
  onLoad: function (options) {

  },
  choose1:function(){
    this.setData({
      step:2,
      array: this.getFields(this.data.caixi1, "name")
    })
  },
  choose2: function () {
    this.setData({
      step: 2,
      array: this.getFields(this.data.caixi2, "name")
    })
  },
  s3: function () {
    this.setData({
      step: 3,
    })
    this.getfoods();
  },
  bindPickerChange(e) {
    this.setData({
      index: e.detail.value
    })
  },
  changeindex:function(){
    this.setData({
      nowid:this.data.nowid+1
    })
  },
  /**
   * 数组提取
   */
  getFields:function(input, field) {
    var output = [];
    for (var i = 0; i < input.length; ++i)
      output.push(input[i][field]);
    return output;
  },
  /**
   * 获取菜品清单
   */
  getfoods:function(){
    if(this.data.array[0]==this.data.caixi1[0].name){
      var cid = this.data.caixi1[this.data.index].id;
    }else{
      var cid = this.data.caixi2[this.data.index].id;
    }
    var that = this;
    let appkey = "4896791dcf8d5cf2489e87dc72ccd7d7";
    let nowid = this.data.nowid;
    wx.request({
      url: 'http://apis.juhe.cn/cook/index',
      method: 'POST',
      header:{ 'content-type': 'application/x-www-form-urlencoded; charset=utf-8' },
      data: {
        key:appkey,
        cid:cid,
        dtype:"json",
        pn:nowid,
        rn:nowid+30,
        format:null,
      },
      success: function (res) {
        console.log(res)
        that.setData({
          foods:res.data.result.data,
          nowid:0
        })

      }
    })
  }
})
